package com.xywl.escort.view.withdraw;

import com.xywl.escort.bean.BandCardListResp;
import com.xywl.escort.bean.BaseReq;
import com.xywl.escort.bean.FindUserCashAmountResp;
import com.xywl.escort.bean.FindUserBankCardListReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2017/10/25.
 */

public class WithdrawPresenter implements WithdrawContract.Presenter {
    private WithdrawContract.View mView;
    private ApiServer mApiServer;

    @Inject
    WithdrawPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void takeView(WithdrawContract.View view) {
        this.mView = view;
    }

    @Override
    public void getBandCardList(String cardType, int pageNum, int pageSize) {
        FindUserBankCardListReq req = new FindUserBankCardListReq();
        req.cardType = cardType;
        req.pageSize = pageSize;
        req.pageNum = pageNum;
        mApiServer.findUserBankCardList(req)
                .compose(RxUtil.<BandCardListResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BandCardListResp>bindToLife())
                .subscribe(new BaseSubscriber<BandCardListResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.getBandCardListFailure();
                    }

                    @Override
                    public void onNext(BandCardListResp resp) {
                        if (null != resp && null != resp.data && resp.isOk()) {
                            mView.getBandCardListSuccess(resp.data);
                        } else {
                            mView.getBandCardListFailure();
                        }
                    }
                });
    }

    @Override
    public void findUserCashAmount(String userId) {
        BaseReq req = new BaseReq();
        req.userId = userId;
        req.userId = userId;
        mApiServer.findUserCashAmount(req)
                .compose(RxUtil.<FindUserCashAmountResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<FindUserCashAmountResp>bindToLife())
                .subscribe(new BaseSubscriber<FindUserCashAmountResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.findUserCashAmountFailure("操作失败");
                    }

                    @Override
                    public void onNext(FindUserCashAmountResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.findUserCashAmountSuccess(resp.data);
                            } else {
                                mView.findUserCashAmountFailure(resp.msg);
                            }
                        } else {
                            mView.findUserCashAmountFailure("操作失败");
                        }
                    }
                });
    }
}
